import sys

from PyQt5.QtWidgets import QWidget, QProgressBar, QPushButton, QApplication, QHBoxLayout
from PyQt5.QtCore import QTimer


class MyProgressBar(QWidget):
    def __init__(self, parent=None):
        super(MyProgressBar, self).__init__(parent)
        self.resize(250, 250)
        self.progress_value = 0

        self.progress = QProgressBar()
        self.btn_start = QPushButton('开始')
        self.btn_stop = QPushButton('暂停')

        hl = QHBoxLayout()
        hl.addWidget(self.progress)
        hl.addWidget(self.btn_start)
        hl.addWidget(self.btn_stop)

        self.setLayout(hl)

        self.progress.setValue(0)

        self.timer = QTimer()
        self.timer.timeout.connect(self.show_progress)
        self.btn_start.clicked.connect(self.timer_start)
        self.btn_stop.clicked.connect(self.timer_stop)

    # 激活定时器
    def timer_start(self):
        if not self.timer.isActive():
            self.timer.start(10)

    # 修改进度条
    def show_progress(self):
        self.progress.setValue(self.progress_value)
        self.progress_value += 1

        if self.progress_value > 100:
            self.timer.stop()

    # 停止定时器
    def timer_stop(self):
        self.timer.stop()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyProgressBar()
    window.show()
    sys.exit(app.exec_())
